package com.google.zxing.client.android.result;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Hashtable;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.xml.sax.SAXException;

import android.content.Context;
import android.util.Log;

public class Question {

	// list index question in file XML
	private ArrayList<String> numbAnswer;

	// get data in database
	private InputStream inStream;

	/*
	 * array contain question object contain all attribute about content
	 * question, content answer, content answer true, content answer user choice
	 */
	private ArrayList<ContentQuestions> arrlstContQues;

	// list of all questions in database
	private ArrayList<ArrayList<Hashtable<String, String>>> arrLstAllContent;

	public Question(Context ctx) {

		// initial arraylists
		arrlstContQues = new ArrayList<ContentQuestions>();
		arrLstAllContent = new ArrayList<ArrayList<Hashtable<String, String>>>();
		numbAnswer = new ArrayList<String>();

//		this.intNumQues = intNumQues;

		// initialize numbAnswer for shuffling
		for (int i = 1; i < 5; i++) {
			numbAnswer.add(i + "");
		}

		// initialize XMLParser object for parsing data
		XMLParser parse = new XMLParser();

//		inStream = ctx.getResources().openRawResource(
//				R.raw.reading_database);
		
		FileInputStream fInputStream = null ;
		
		try {
			fInputStream = ctx.openFileInput("database.xml");
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		inStream = (InputStream)fInputStream;
		
		// initialize DOM parser
		DocumentBuilder builder;

		try {

			builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();

			Document doc = builder.parse(inStream, null);

			// parse data from doc
			parse.Parse(doc);

			arrLstAllContent = parse.getAllQuestion();

		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

			addListContent();
	}

	// get and set function for varibles
	public ArrayList<ArrayList<Hashtable<String, String>>> getAllQuestion() {
		return arrLstAllContent;
	}

	public void setAllQuestion(
			ArrayList<ArrayList<Hashtable<String, String>>> _lstAllQuestion) {
		arrLstAllContent = _lstAllQuestion;
	}

	// random index of question in list all question
	public void addListContent() {
		// add attribute into array contain question
		if (arrLstAllContent.size() >= 0) {
			for (int i = 0; i < arrLstAllContent.size(); i++) {
				
				addAttributeArrLstContQues(i);
			}
		}
				
					/*for (int i = 0; i < arrlstAllQuestion.size(); i++) {
						// add attribute into array contain question
						addAttributeArrLstContQues(i);
					}*/

	}

	public void getQuestionWithLevel(String intLevelPlay, int numberQuestion) {
		Collections.shuffle(arrLstAllContent);
		String strCheckCondition = new String();
		for (int i = 0; i < arrLstAllContent.size(); i++) {
			strCheckCondition = arrLstAllContent.get(i).get(0)
					.get(intLevelPlay);
			if (arrlstContQues.size() >= numberQuestion) {
				break;
			}
			if (strCheckCondition != null) {
				// add attribute into array contain question
				addAttributeArrLstContQues(i);
			}
		}
		/*for (int i = 0; i < arrlstAllQuestion.size(); i++) {
			// add attribute into array contain question
			addAttributeArrLstContQues(i);
		}*/
	}

	public void addAttributeArrLstContQues(int i) {
		// add attribute into array contain question
		ContentQuestions contQues = new ContentQuestions();

		// add content question
		contQues.setStrQues(arrLstAllContent.get(i).get(0).values().toString()
				.replaceAll("[\\[\\]]", ""));
		contQues.setStrAnsTrue(arrLstAllContent.get(i).get(1).get("1"));
//		Collections.shuffle(numbAnswer);
		contQues.setStrAnsA(arrLstAllContent.get(i).get(1)
				.get(numbAnswer.get(0)));
		contQues.setStrAnsB(arrLstAllContent.get(i).get(1)
				.get(numbAnswer.get(1)));
		contQues.setStrAnsC(arrLstAllContent.get(i).get(1)
				.get(numbAnswer.get(2)));
		contQues.setStrAnsD(arrLstAllContent.get(i).get(1)
				.get(numbAnswer.get(3)));
		arrlstContQues.add(contQues);
	}

	public ArrayList<ContentQuestions> getArrlstContQues() {
		return arrlstContQues;
	}

	public void setArrlstContQues(ArrayList<ContentQuestions> arrlstContQues) {
		this.arrlstContQues = arrlstContQues;
	}
}
